<?php
/**
 * 葫芦侠功能操作类
 *
 * 封装了葫芦侠的常用功能
 *
 * @author: HuajiMC
 */

class Huluxia {
	private $key;
	
	/**
	 * 构造方法
	 * 
	 * @param $p1 参数一
	 * @param $p2 default null 参数二
	 * 
	 * @return boolean
	 */
	public function __construct($p1,$p2 = null) {
		if(empty($p2)) {
			if($this->checkLogin($p1)) {
				$this->key = $p1;
			}
		} else {
			$login = $this->login($p1,$p2);
			if($login['status']) {
				$this->key = $login['_key'];
			}
		}
	}
	
	/**
	 * 获取用户信息
	 * 
	 * @param $userId 用户ID
	 * 
	 * @return array 用户信息
	 */
	public function getUserInfo($userId) {
		$user = $this->getApi("http://floor.huluxia.com/user/info/ANDROID/2.1?user_id={$userId}");
		return $user;
	}
	
	/**
	 * 获取消息列表
	 * 
	 * @param $type default 0 类型（8-帖子；1-系统）
	 * @param $count default 20 数量
	 * @param $start default 0 start值
	 * 
	 * @return array 消息列表
	 */
	public function getMessage($type = 8,$count = 20,$start = 0) {
		$message = $this->getApi("http://floor.huluxia.com/message/new/list/ANDROID/4.1?type_id={$type}&count={$count}&start={$start}");
		return $message["datas"];
	}
	
	/**
	 * 获取帖子信息
	 * 
	 * @param $postId 帖子ID
	 * @param $page 页码
	 * 
	 * @return array 帖子信息
	 */
	public function getPostInfo($postId,$page = 1) {
		if($page == -1) $page = $this->getPostInfo($postId)["totalPage"];
		$post = $this->getApi("http://floor.huluxia.com/post/detail/ANDROID/2.3?post_id={$postId}&page_no={$page}");
		return $post;
	}
	
	/**
	 * 获取楼主模式帖子信息
	 * 
	 * @param $postId 帖子ID
	 * @param $page 页码
	 * 
	 * @return array 帖子信息
	 */
	public function getPostFloorInfo($postId,$page = 1) {
		if($page == -1) $page = $this->getPostFloorInfo($postId)["totalPage"];
		$post = $this->getApi("http://floor.huluxia.com/post/detail/floor/ANDROID/2.3?post_id={$postId}&page_no={$page}");
		return $post;
	}
	
	/**
	 * 获取帖子打赏列表
	 * 
	 * @param $postId 帖子ID
	 * 
	 * @return array 打赏列表
	 */
	public function getPostCreditsList($postId) {
		$credits = $this->getApi("http://floor.huluxia.com/credits/all/ANDROID/2.0?post_id={$postId}&count=100000&type_id=1");
		return $credits['scorelist'];
	}
	
	/**
	 * 获取评论打赏列表
	 * 
	 * @param $commentId 评论ID
	 * 
	 * @return array 打赏列表
	 */
	public function getCommentCreditsList($commentId) {
		$credits = $this->getApi("http://floor.huluxia.com/credits/all/ANDROID/2.0?post_id={$commentId}&count=100000&type_id=2");
		return $credits['scorelist'];
	}
	
	/**
	 * 获取版块帖子列表
	 *
	 * @param $catId 版块ID
	 * @param $tagId default 0 分类ID
	 * @param $sort default 0 排序方式（[0]回复时间,[1]发布时间,[2]本版精华）
	 * @param $count default 20 数量
	 * @param $start default 0 start值
	 *
	 * @return array 帖子列表
	 */
	public function getCategoryPostList($catId,$tagId = 0,$sort = 0,$count = 20,$start = 0) {
		$result = $this->getApi("http://floor.huluxia.com/post/list/ANDROID/2.1?cat_id={$catId}&tag_id={$tagId}&sort_by={$sort}&count={$count}&start={$start}");
		foreach($result['posts'] as &$post) {
			$post['more'] = $result['more'];
			$post['start'] = $result['start'];
		}
		return $result['posts'];
	}
	
	/**
	 * 获取版块置顶列表
	 *
	 * @param $catId 版块ID
	 *
	 * @return array 帖子列表
	 */
	public function getCategoryTopPostList($catId) {
		$result = $this->getApi("http://floor.huluxia.com/post/list/ANDROID/2.1?cat_id={$catId}");
		return $result['weightAndTopPost'];
	}
	
	/**
	 * 获取版块信息
	 * 
	 * @param $catId 版块ID
	 * 
	 * @return array 版块信息
	 */
	public function getCategoryInfo($catId) {
		$category = $this->getApi("http://floor.huluxia.com/category/detail/ANDROID/2.0?cat_id={$catId}");
		return $category;
	}
	
	/**
	 * 获取版块列表
	 * 
	 * @return array 版块列表
	 */
	public function getCategoryList() {
		$categoryList = $this->getApi("http://floor.huluxia.com/category/list/ANDROID/2.0?");
		return $categoryList['categories'];
	}
	
	/**
	 * 获取轮播列表
	 * 
	 * @return array 帖子列表
	 */
	public function getSlideList() {
		$postList = $this->getApi("http://floor.huluxia.com/post/slide/ANDROID/4.1?");
		return $postList['postList'];
	}
	
	/**
	 * 关注用户
	 * 
	 * @return array 关注接口返回信息
	 */
	public function follow($userId) {
		$follow = $this->getApi("http://floor.huluxia.com/friendship/follow/ANDROID/2.0?user_id={$userId}");
		return $follow;
	}
	
	/**
	 * 取关用户
	 * 
	 * @return array 取关接口返回信息
	 */
	public function unfollow($userId) {
		$follow = $this->getApi("http://floor.huluxia.com/friendship/unfollow/ANDROID/2.0?user_id={$userId}");
		return $follow;
	}
	
	/**
	 * 获取帖子评论信息
	 * 
	 * @param $postId 帖子ID
	 * @param $floor 楼层（-1 为最后一楼）
	 * 
	 * @return array 评论信息
	 */
	public function getCommentInfo($postId,$floor) {
		if($floor != -1) {
			$page = ceil($floor / 20);
			$post = $this->getPostInfo($postId,$page);
			$index = $floor - ($page - 1) * 20 - 1;
			$comment = $post["comments"][$index];
		} else {
			$post = $this->getPostInfo($postId,-1);
			$comment = end($post["comments"]) ?: [];
		}
		return $comment;
	}
	
	/**
	 * 获取用户帖子列表
	 * 
	 * @param $userId 用户ID
	 * @param $count 帖子数量
	 * @param $start default 0 start值
	 * 
	 * @return array 帖子列表
	 */
	public function getUserPostList($userId,$count,$start = 0) {
		$postList = $this->getApi("http://floor.huluxia.com/post/create/list/ANDROID/2.0?user_id={$userId}&count={$count}&start={$start}");
		return $postList["posts"];
	}
	
	/**
	 * 获取用户收藏列表
	 * 
	 * @param $userId 用户ID
	 * @param $count 帖子数量
	 * @param $start default 0 start值
	 * 
	 * @return array 帖子列表
	 */
	public function getUserFavoriteList($userId,$count,$start = 0) {
		$postList = $this->getApi("http://floor.huluxia.com/post/favorite/list/ANDROID/2.0?user_id={$userId}&count={$count}&start={$start}");
		return $postList["posts"];
	}
	
	/**
	 * 获取用户评论列表
	 * 
	 * @param $userId 用户ID
	 * @param $count 评论数量
	 * @param $start default 0 start值
	 * 
	 * @return array 评论列表
	 */
	public function getUserCommentList($userId,$count,$start = 0) {
		$postList = $this->getApi("http://floor.huluxia.com/comment/create/list/ANDROID/2.0?user_id={$userId}&count={$count}&start={$start}");
		return $postList["comments"];
	}
	
	/**
	 * 获取用户关注列表
	 * 
	 * @param $userId 用户ID
	 * @param $count 数量
	 * @param $start default 0 start值
	 * 
	 * @return array 关注列表
	 */
	public function getUserFollowList($userId,$count,$start = 0) {
		$userList = $this->getApi("http://floor.huluxia.com/friendship/following/list/ANDROID/2.0?user_id={$userId}&count={$count}&start={$start}");
		return $userList["friendships"];
	}
	
	/**
	 * 获取用户粉丝列表
	 * 
	 * @param $userId 用户ID
	 * @param $count 数量
	 * @param $start default 0 start值
	 * 
	 * @return array 关注列表
	 */
	public function getUserFansList($userId,$count,$start = 0) {
		$userList = $this->getApi("http://floor.huluxia.com/friendship/follower/list/ANDROID/2.0?user_id={$userId}&count={$count}&start={$start}");
		return $userList["friendships"];
	}
	
	/**
	 * 发布评论
	 * 
	 * @param $postId 帖子ID
	 * @param $commentId 回复的评论ID（没有填0）
	 * @param $text 评论内容
	 * @param $images default array() 图片
	 * 
	 * @return array 评论接口返回数据
	 */
	public function createComment($postId,$commentId,$text,$images = array()) {
		$images = implode(",",$images);
		$text = urlencode($text);
		
		$result = $this->getApi("http://floor.huluxia.com/comment/create/ANDROID/2.0?post_id={$postId}&comment_id={$commentId}&text={$text}&images={$images}");
		return $result;
	}
	
	/**
	 * 发布帖子
	 * 
	 * @param $catId 版块ID
	 * @param $tagId 标签ID（无标签版块填-1）
	 * @param $title 帖子标题
	 * @param $detail 帖子内容
	 * @param $images default array() 图片
	 * @param $atUsers default array() @的用户（用户ID数组）
	 * 
	 * @return array 发帖接口返回数据
	 */
	public function createPost($catId,$tagId,$title,$detail,$images = array(),$atUsers = array()) {
		$images = implode(",",$images);
		$atUsers = implode(",",$atUsers);
		$title = urlencode($title);
		$detail = urlencode($detail);
		
		$result = $this->getApi("http://floor.huluxia.com/post/create/ANDROID/2.1?cat_id={$catId}&tag_id={$tagId}&title={$title}&detail=<text>{$detail}</text>&images={$images}&user_ids={$atUsers}");
		return $result;
	}
	
	/**
	 *
	
	/**
	 * 根据key获取用户ID
	 * 
	 * @return int 用户ID
	 */
	public function getUserIdByKey($key = null) {
		$key = $key ?? $this->key;
		$result = $this->getApi("http://floor.huluxia.com/product/medal/list/ANDROID/2.1?_key={$key}");
		return $result['user']['userID'];
	}
	
	/**
	 * 删除评论
	 * 
	 * @param $postId 帖子ID
	 * @param $floor 楼层
	 * 
	 * @return array 接口返回信息
	 */
	public function deleteComment($postId,$commentID,$floor = null) {
		$commentID = $floor ? $this->getCommentInfo($post,$floor) : $commentID;
		$result = $this->getApi("http://floor.huluxia.com/comment/destroy/ANDROID/2.0?comment_id={$commentID}");
		return $result;
	}
	
	/**
	 * 验证key是否有效
	 * 
	 * @param $key key
	 * 
	 * @return boolean
	 */
	public function checkLogin($key = null) {
		$key = $key ?? $this->key;
		if($this->getApi("http://floor.huluxia.com/sdk/account/profile/ANDROID/4.0?_key={$key}")["msg"] != "未登录") {
			return true;
		}
		return false;
	}
	
	/**
	 * 账号密码登录
	 *
	 * @param $phone 手机号
	 * @param $password 密码
	 *
	 * @return array 接口返回信息
	 */
	public function login($phone,$password) {
		$result = $this->getApi("http://floor.huluxia.com/account/login/ANDROID/4.0?account={$phone}&password=" . md5($password) . "&login_type=2&device_code=滑稽mc");
		return $result;
	}
	
	/**
	 * 版块签到
	 * 
	 * @param $catId 版块ID
	 * 
	 * @return array 接口返回信息
	 */
	public function signin($catId) {
		$result = $this->getApi("http://floor.huluxia.com/user/signin/ANDROID/4.0?cat_id=" . $catId);
		return $result;
	}
	
	/**
	 * 版块签到检验
	 * 
	 * @param $catId 版块ID
	 * 
	 * @return boolean true/false
	 */
	public function signinCheck($catId) {
		$result = $this->getApi("http://floor.huluxia.com/user/signin/check/ANDROID/2.0?cat_id=" . $catId);
		return (bool) $result['signin'];
	}
	
	/**
	 * 赠送葫芦
	 * 
	 * @param $postId 帖子ID
	 * @param $reason 原因
	 * @param $floor default 0 楼层
	 * @param $score default 1 数量
	 * 
	 * @return array 接口返回信息
	 */
	public function transfer($postId,$reason,$floor = 0,$score = 1) {
		$postId = $floor != 0 ? $this->getCommentInfo($postId,$floor)['commentID'] : $postId;
		$type = $floor != 0 ? 2 : 1;
		$reason = urlencode($reason);
			
		$result = $this->getApi("http://floor.huluxia.com/credits/transfer/ANDROID/2.0?post_id={$postId}&type_id={$type}&isadmin=0&score={$score}&score_txt={$reason}");
		return $result;
	}
	
	/**
	 * 获取KEY
	 * 
	 * @return string key
	 */
	public function getKey() {
		return $this->key;
	}
	
	/**
	 * 获取接口返回信息
	 * 
	 * @param $url 接口地址
	 * @param $array default true 是否将返回的json转换成数组
	 * 
	 * @return array or string 接口返回的json或转换后的数组
	 */
	public function getApi($url,$array = true) {
		$url .= "&_key=" . $this->key;
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_HTTPHEADER,["User-Agent:okhttp/3.8.1"]);
		$response = curl_exec($ch);
		curl_close($ch);
		return $array ? json_decode($response,true) : $response;
	}
}